package com.yym;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.protobuf.ByteString;
import org.hyperledger.fabric.shim.ChaincodeBase;
import org.hyperledger.fabric.shim.ChaincodeStub;
import org.hyperledger.fabric.shim.ResponseUtils;
import org.hyperledger.fabric.shim.ledger.KeyModification;
import org.hyperledger.fabric.shim.ledger.QueryResultsIterator;

import java.util.List;

/**
 * @author yeym
 */
public class ChainCode extends ChaincodeBase {
    @Override
    public Response init(ChaincodeStub stub) {
        System.out.println("--------init--------");
        return ResponseUtils.newSuccessResponse();
    }

    @Override
    public Response invoke(ChaincodeStub stub) {
        System.out.println("--------invoke--------");
        String fcn = stub.getFunction();
        List<String> args = stub.getParameters();
        System.out.format("fcn => %s", fcn);
        System.out.println();
        switch (fcn) {
            case "save":
                return save(stub, args);
            case "query":
                return query(stub, args);
            case "queryHis":
                return queryHis(stub, args);
        }
        return ResponseUtils.newErrorResponse("unimplemented method => " + fcn);
    }

    private Response save(ChaincodeStub stub, List<String> args) {
        if (args.size() != 2) {
            return ResponseUtils.newErrorResponse("参数异常");
        }
        try {
            String key = args.get(0);
            System.out.println(key);
            String value = args.get(1);
            System.out.println(value);
            stub.putStringState(key, value);
            return ResponseUtils.newSuccessResponse("save successful");
        } catch (Exception e) {
            return ResponseUtils.newErrorResponse("未知错误");
        }
    }



    private Response query(ChaincodeStub stub, List<String> args) {
        if (args.size() != 1) {
            return ResponseUtils.newErrorResponse("参数错误");
        }
        String value = stub.getStringState(args.get(0));
        return ResponseUtils.newSuccessResponse(value, ByteString.copyFromUtf8(value).toByteArray());
    }

    private Response queryHis(ChaincodeStub stub, List<String> args) {
        System.out.println(args.get(0));
        QueryResultsIterator<KeyModification> historyForKey = stub.getHistoryForKey(args.get(0));
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject;
        for (KeyModification keyModification : historyForKey) {
            jsonObject = new JSONObject();
            jsonObject.put("time", keyModification.getTimestamp().getEpochSecond());
            jsonObject.put("value", keyModification.getStringValue());
            jsonObject.put("txId", keyModification.getTxId());
            jsonObject.put("isDelete", keyModification.isDeleted());
            jsonArray.add(jsonObject);
        }
        System.out.println(jsonArray);
        return ResponseUtils.newSuccessResponse(jsonArray.toJSONString(), ByteString.copyFromUtf8(jsonArray.toJSONString()).toByteArray());
    }

    private static void startChainCode(String... args) {
        new ChainCode().start(args);
    }

    public static void main(String[] args) {
        //指定地址和调用参数
        startChainCode(args);
    }

}